package com.ruoyi.common.config.mybatis;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
import com.baomidou.mybatisplus.core.injector.methods.*;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.ruoyi.common.config.mybatis.method.Delete;
import com.ruoyi.common.config.mybatis.method.DeleteBatchByIds;
import com.ruoyi.common.config.mybatis.method.DeleteById;
import com.ruoyi.common.config.mybatis.method.DeleteByMap;
import com.ruoyi.common.config.mybatis.method.*;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

/**
 * @Classname CustomSqlInjector
 * @Author: sunluquan
 * @Date: 2021/12/23 16:17
 * @Description TODO
 * 自定义sql语句注入器
 */
@Component
public class CustomSqlInjector extends AbstractSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {

        List<AbstractMethod> methodList = new ArrayList<>();

        if (tableInfo.havePK()) {
            methodList.addAll(Stream.of(
                    new Insert(),
                    new Delete(),
                    new DeleteByMap(),
                    new DeleteById(),
                    new DeleteBatchByIds(),
                    new Update(),
                    new UpdateById(),
                    new SelectById(),
                    new SelectBatchByIds(),
                    new SelectByMap(),
                    new SelectCount(),
                    new SelectMaps(),
                    new SelectMapsPage(),
                    new SelectObjs(),
                    new SelectList(),
                    new SelectPage()
            ).collect(toList()));
        } else {
            logger.warn(String.format("%s ,Not found @TableId annotation, Cannot use Mybatis-Plus 'xxById' Method.",
                    tableInfo.getEntityType()));
            methodList.addAll(Stream.of(
                    new Insert(),
                    new Delete(),
                    new DeleteByMap(),
                    new Update(),
                    new SelectByMap(),
                    new SelectCount(),
                    new SelectMaps(),
                    new SelectMapsPage(),
                    new SelectObjs(),
                    new SelectList(),
                    new SelectPage()
            ).collect(toList()));
        }
        if(tableInfo.isWithLogicDelete()){
            methodList.add(new LogicDelete());
            methodList.add(new LogicDeleteById());
            methodList.add(new LogicDeleteBatchByIds());
            methodList.add(new RecoverById());
            methodList.add( new RecoveryBatchByIds());
        }

        return methodList;
    }
}
